import 'package:flutter/material.dart';
import 'package:tiktokadult/hj_utils/screen.dart';

import '../../hj_model/home/video_list_resp.dart';
import '../../hj_model/video_model.dart';
import '../../hj_utils/api_service/vid_service.dart';
import '../../tools_base/debug_log.dart';
import '../../tools_base/loading/l_error_widget.dart';
import '../../tools_base/loading/loading_center_widget.dart';
import 'find_view/short_find_table_view.dart';

class ShortVideoFindPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ShortVideoFindPageState();
  }
}

class _ShortVideoFindPageState extends State<ShortVideoFindPage> with TickerProviderStateMixin {
  static List<TagsBean>? cacheDataArr;
  List<TagsBean>? dataArr;
  TabController? tabController;

  @override
  void initState() {
    super.initState();
    if (cacheDataArr?.isNotEmpty == true) {
      dataArr = cacheDataArr;
      tabController = TabController(length: dataArr?.length ?? 0, vsync: this);
    } else {
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
        _loadData();
      });
    }
  }

  void _loadData({int page = 1, int size = 10}) async {
    try {
      VideoListResp? resp = await VidService.shortFindTabList(page, size, type: 1);
      dataArr ??= [];
      dataArr?.add(TagsBean(name: "最热"));
      dataArr?.add(TagsBean(name: "最新"));
      dataArr?.addAll(resp?.tags ?? []);
      cacheDataArr = dataArr;
      tabController = TabController(length: dataArr?.length ?? 0, vsync: this);
    } catch (e) {
      dataArr ??= [];
      debugLog(e);
    }
    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    if (dataArr == null) {
      return LoadingCenterWidget();
    } else if (dataArr!.isEmpty) {
      return CErrorWidget(
        retryOnTap: () {
          dataArr = null;
          setState(() {});
          _loadData();
        },
      );
    } else {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            padding: EdgeInsets.only(bottom: 6),
            alignment: Alignment.center,
            width: screen.screenWidth,
            child: TabBar(
              indicator: BoxDecoration(),
              labelColor: Colors.white,
              labelStyle: TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.w500,
                height: 1.3,
              ),
              unselectedLabelColor: Colors.white.withOpacity(0.35),
              unselectedLabelStyle: const TextStyle(
                fontSize: 14,
                height: 1.3,
              ),
              labelPadding: EdgeInsets.zero,
              controller: tabController,
              isScrollable: true,
              tabAlignment: TabAlignment.start,
              tabs: dataArr!
                  .map((e) => Container(
                        padding: EdgeInsets.fromLTRB(12, 0, 12, 2),
                        child: Text(e.name ?? ""),
                      ))
                  .toList(),
              indicatorPadding: EdgeInsets.zero,
            ),
          ),
          Expanded(
            child: TabBarView(
              controller: tabController,
              children: List.generate(
                dataArr!.length,
                (index) {
                  return ShortFindTableView(model: dataArr![index]);
                },
              ).toList(),
            ),
          ),
        ],
      );
    }
  }
}
